<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Wechat\Controller;
use Think\Controller;
use Wechatlib\WechatAuth;
use Wechatlib\WechatJsSdk;

class ExpertController extends Controller{
    
	protected $wechat = null;
    protected $appID = '';        // add for jssdk
    protected $appSecret = '';    // add for jssdk
	
    protected function getAppIdSec(){
        $list = M("Config")->where(array('status'=>1))->field('name,value')->order('sort')->select();
		foreach($list as $v){
			if($v['name'] == 'WECHAT_APPID'){
				$this->appID = $v['value'];
			}elseif($v['name'] == 'WECHAT_APPSECRET'){
				$this->appSecret = $v['value'];
			}
		}
    }
    
	protected function init_wechat(){
		if($this->wechat){
			return true;
		}
		
		$this->getAppIdSec();
		$this->wechat = new WechatAuth($this->appID, $this->appSecret);
	}
    
    /**
     * @brief   调用微信接口发送模板消息
     * @func_get_arg sendTemplateMessage($data, $touser, $templateId, $url, $topcolor='#FF0000')
     */
    protected function sendTplMsg($regdata, $openid) {
        $this->init_wechat();
        
        $templateId = 'XbXr8X-IjQrExyzs8459xRboz6ehPvITWw8kc5PWVdg';    // 专家注册后的消息推送
        
        $url = 'http://u_3172998.zuodanye.maka.im/viewer/O9R1H26C';
        
        $first = '您好，有新需求提交，请注意及时跟进！ 客户联系电话： ' . $regdata['contact_number'] . 
                    '， 客户公司名称：' . $regdata['company_name'] . '\n';
        
        $data = array(
            'first'=>array('value'=>$first, 'color'=>'#424242'),
            'keyword1'=>array('value'=>'\n'.$regdata['detail'].'\n', 'color'=>'#0000EE'),       // 需求内容
            'keyword2'=>array('value'=>' ' . $regdata['contact_name'], 'color'=>'#EE7600'),
            'keyword3'=>array('value'=>' '. date("Y-m-d H:i:s"), 'color'=>'#EE7600'),
            'remark'=>array('value'=>'\n请及时确认需求并与客户沟通，原则上不超过1个工作日！', 'color'=>'#2B2B2B')
        );
        
        $this->wechat->sendTemplateMessage($data, $openid, $templateId, $url, $topcolor='#FF0000');
    }
    
	
	/**
     * 提交需求
     */
    public function request(){
		$this->assign('title', '提交服务需求');
		
		if(IS_POST){
			$post_tag = session('post_tag');
			if($post_tag != 'request'){
				$this->redirect('expert/request');
				return;
			}
			
			$data = $_POST;
			if($data['contact_name'] && $data['contact_number']){
				$data['submit_time'] = time();
                
				$id = M('requirements')->add($data);
			}else{
				$id = false;
			}
            
			if($id){
				session('post_tag', null);
				
				$this->assign('status', 'success');
				$this->assign('msg_title', '操作成功');
				$this->assign('title', '操作成功');
				$this->assign('msg_desc', '感谢您提交需求，工作人员将尽快与您联系！');
                
                // 收到需求后，发送微信模板消息
                $this->sendTplMsg($data, 'ofGK9wmXX6qg2GIi7fJNv9YFqAHk');   // csz
                
				$this->assign('msg_url', 'http://u_3172998.zuodanye.maka.im/viewer/O9R1H26C');
			}else{
				$this->assign('status', 'warn');
				$this->assign('msg_title', '操作失败');
				$this->assign('title', '操作失败');
				$this->assign('msg_desc', '请填写必填项或联系平台，抱歉。');
				$this->assign('msg_url', U('Expert/request'));
			}
			
			$this->display('request_result');
		}else{
            $this->init_wechat();
        
            $jssdk = new WechatJsSdk($this->appID, $this->appSecret, $this->wechat->accessToken);
            $signPackage = $jssdk->GetSignPackage();
            $shareInfo = array(
                'title' => '固拓快包 - 提交服务需求',
                'link' => "http://wx.goomtu.com/Wechat/Expert/request.html",
                'desc' => '您可点击此链接简要录入需求，提交后平台将尽快与您联系，感谢使用固拓快包！',
                'imgUrl' => 'http://wx.goomtu.com/logo.png'
            );
            
            // 增加字段用于分享
            $this->assign('signPackage', $signPackage);
            $this->assign('shareInfo', $shareInfo);
			
			session('post_tag', 'request');
			$this->display('request');
		}
    }
	
    
    /*
	public function update(){
		//
		$list = M('experts')->where('expert_certificate>0')->field('id')->select();
		foreach($list as $row){
            $id = $row['id'];
            
			$update = array(
				'completeness' => 5,
			);
			M('experts')->where("id=$id")->save($update);
		}
		
		echo 'ok';
	}
    */
}
?>